/***********************************************************************************************************************
* Copyright (C) . All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : main.c
* Version      :  
* Device(s)    : BAT32G137
* Tool-Chain   : MDK(armcc)
* Description  : This file is a template.
* Creation Date: 2019/4/30
***********************************************************************************************************************/

/***********************************************************************************************************************
Macro Definitions
***********************************************************************************************************************/

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include <stdio.h>
#include "BAT32G137.h"
#include "gpio.h"
#include "key.h"

/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
volatile uint32_t g_ticks;

void delayMS(uint32_t n)
{
		g_ticks = n;
		while(g_ticks);
}

int main()
{
	uint32_t msCnt; 	// count value of 1ms
	uint32_t i;
	
//-----------------------------------------------------------------------
// Systick setting 
//-----------------------------------------------------------------------   
	g_ticks = 1000; 	// 1000ms
	SystemCoreClockUpdate();
	msCnt = SystemCoreClock / 1000;
	 
//-----------------------------------------------------------------------
// Enable KR0 falling edge interrupt request to wakeup system
//-----------------------------------------------------------------------   
	KEY_Init(1 << 0); 			// Initializes P70/KR0
	KEY_Start(); 	            // Enable KEY Interrupt
#if 0
	__set_PRIMASK(1);			// Disable exceiptions(interrupt request wakeup cpu, 
								// But DON'T entry interrupt service routine.)
#endif

//-----------------------------------------------------------------------
// DeepSleep 10 times
// P70/KR0 falling edge wakeup system and blinky P71/LED (D5)
//-----------------------------------------------------------------------
	// LED ON
	GPIO_Output_Enable(&PORT->P7, 3 << 1); // P71, P72 output enable
	for(i=10; i>0; )
	{
		SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
		__WFI();
		SysTick_Config(msCnt);
		delayMS(5); 	// Filter out key jitter
		SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
		if(0U == (PORT->P7 & 0x01))
		{
			PORT->P7 ^= (1 << 1); 	// P71 toggle 
			INTC_ClearPendingIRQ(KEY_IRQn);
			NVIC_ClearPendingIRQ(KEY_IRQn);
			i--;
		}
	}

//-----------------------------------------------------------------------
// LED blinky on target board 
// P72 drives LED on board
// P71 drives LED on board
//-----------------------------------------------------------------------
	SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;	
	PORT->P7    = 0x02U;  
	PORT->PM7  &= ~(3<<1); // P72/LED-D4, P71/LED-D5

	while(1)
	{
		delayMS(250);
		PORT->P7 ^= (3<<1); 	// Toggle P71, P72
	}
								
}

/***********************************************************************************************************************
* Function Name: SysTick Handler
* Description  : Decreament the g_ticks value
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void SysTick_Handler(void)
{
	g_ticks--;
}

